const app = getApp()
import { getPprojectIslandDetail } from '../../../../api/index'
import { formatTimeTwo } from '../../../../utils/util'

Page({
  data: {
    tabActive: 'detail',
    projectId: '',
    detail: {},
    safeBottom: 0,
    roleIds: [],
    timestamp: 0
  },
  changeTab ({ currentTarget: { dataset: { value } } }) {
    this.setData({ tabActive: value })
    this.getRequest()
  },
  async confirmJoin () {
    try {
      if (!this.data.sections.length) {
        wx.showToast({ title: '请先选择材料册！', icon: 'none' })
        return
      }
      let ids = this.data.sections.map(item => { return item.bookNo })
      await intoMaterialBook({ projectId: this.data.projectId, bookNos: ids })
      wx.showToast({ title: '入册成功！', icon: 'none' })
      this.getMaterialBook()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getRequest () {
    if (this.data.tabActive === 'detail') {
      this.getDetail()
    }
  },
  async getDetail () {
    try {
      wx.showLoading({ title: '加载中...', mask: true })
      const { ResultSet } = await getPprojectIslandDetail({ project_id: this.data.projectId })
      ResultSet.userData.created_at = formatTimeTwo(ResultSet.userData.created_at, 'Y-M-D')
      if (ResultSet.userData.contact) ResultSet.userData.contact = JSON.parse(ResultSet.userData.contact)
      this.setData({ detail: ResultSet.userData })
      wx.hideLoading()
    } catch (err) {
      wx.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad ({ id, roleids }) {
    const { screenHeight, safeArea: { bottom } } = wx.$pro.getSystemInfoSync()
    this.setData({ safeBottom: screenHeight - bottom, projectId: id, roleIds: roleids.split(',') })
  },
  onShow () {
    this.setData({ timestamp: +new Date() })
    this.getRequest()
  }
})